package com.boco.raptor.subsystem.attempx.actionHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.boco.common.util.debug.LogHome;
import com.boco.common.util.except.UserException;
import com.boco.raptor.common.service.IServiceActionContext;
import com.boco.raptor.drm.core.dto.DrmEntityFactory;
import com.boco.raptor.drm.core.dto.IDrmLabelValue;
import com.boco.raptor.drm.core.dto.IDrmQueryContext;
import com.boco.raptor.drm.core.dto.IDrmQueryResultSet;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.common.dto.base.GenericDO;
import com.boco.transnms.server.dao.base.AbstractDaoName;
import com.boco.transnms.server.dao.base.DaoHomeFactory;
import com.boco.transnms.server.dao.base.IDataAccessObject;

public class AttempXHelperAction {   
    
    public IDrmQueryResultSet getObjectsBySql(HttpServletRequest request,
			IDrmQueryContext queryContext, Map<String, String> queryCond)
			throws UserException {
		IDrmQueryResultSet rs = null;
		try {
			String sql = queryCond.get("sql");
			String dbClassIds = queryCond.get("dbClassIds");
			String[] _dbClassIds = dbClassIds.split(",");
			GenericDO[] dboTemplates = new GenericDO[_dbClassIds.length];
			for (int i = 0; i < _dbClassIds.length; i++) {
				GenericDO dboTemplate = new GenericDO(_dbClassIds[i]);
				dboTemplates[i] = dboTemplate.createInstanceByClassName();
			}
  
			rs = getGenericDAO().selectDBOs(AttempXHelper.drmQryCxt2boQryCxt(queryContext),
					sql, dboTemplates);
		} catch (Exception ex) {
			LogHome.getLog().error("", ex);
			throw new UserException(ex.getMessage());
		}
		return rs;
	}
    
    public Map getObjectById(HttpServletRequest request, String dbClassId, String id, boolean isCuid) throws UserException{
        GenericDO dbo = new GenericDO(dbClassId);
        try {
            dbo = dbo.createInstanceByClassName();
            if(isCuid){
                dbo.setCuid(id);
                dbo = getGenericDAO().getObjByCuid(dbo);
            }else{
                dbo.setObjectId(id);
                dbo = getGenericDAO().getObject(dbo);
            }
        } catch (Exception ex) {
            LogHome.getLog().error("", ex);
            dbo = null;
        }
        return dbo == null ? null : dbo.getAllAttr();
    }
    
    public List<IDrmLabelValue> getDbEnums(HttpServletRequest request, String dbClassId){
        List<IDrmLabelValue> enums = new ArrayList();
        try {
            String sql = "select KEY_NUM, KEY_VALUE from " + dbClassId;
            DataObjectList dbos = getGenericDAO().selectDBOs(sql, new Class[]{long.class, String.class});
            for(GenericDO dbo : dbos){
                long value = dbo.getAttrLong("1");
                String label = dbo.getAttrString("2");
                IDrmLabelValue _enum = DrmEntityFactory.getInstance().createLabelValue(label, value);
                enums.add(_enum);
            }

        } catch (Exception ex) {
            LogHome.getLog().error("", ex);
        }
        return enums;
    }
    
    private IDataAccessObject getGenericDAO() {
        return DaoHomeFactory.getInstance().getDAO(AbstractDaoName.GenericDAO);
    }
}
